package com.fundcrawler.service;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.net.URI;

@Service
public class NotificationService {

    @Autowired
    private JavaMailSender mailSender;

    @Async
    public void sendErrorAlert(String subject, String content) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo("1052023708@qq.com");
            message.setSubject("[基金系统] " + subject);
            message.setText(content + "\n\n时间：" + System.currentTimeMillis());
            mailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Async
    public void pushWechat(String title, String content) {
        String key = System.getenv("SERVER_CHAN_KEY"); // 推荐用环境变量
        if (key == null) key = "YOUR_SCT_KEY"; // 替换为你的 Server 酱 key
        String url = "https://sctapi.ftqq.com/" + key + ".send";
        try {
//            HttpClient.newHttpClient().send(
//                HttpRequest.newBuilder()
//                    .uri(URI.create(url))
//                    .header("Content-Type", "application/x-www-form-urlencoded")
//                    .POST(HttpRequest.BodyPublishers.ofString("title=" + title + "&desp=" + content))
//                    .build(),
//                HttpResponse.BodyHandlers.ofString()
//            );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
